package com.whut.controller;

import com.whut.model.*;
import com.whut.service.EquipService;
import com.whut.utils.FileUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author chr
 * @Date 2020/6/1910:52
 * @ClassName EquipController
 * @Description TODO
 */
@RestController
@RequestMapping("/equip")
public class EquipController {
    @Resource
    private EquipService equipService;

    /**
     * @Author chenhuanru
     * @Description //获取单台设备分数及该类设备分数
     * @Date 17:54 2020/6/21
     * @Param [equipNums, checkId, fourthId]
     * @return java.lang.Object
     **/
    @PostMapping("/getEquipCheckResult")
    public Object getEquipCheckResult(String equipNums,int checkId,int fourthId){

        return equipService.getEquipCheckResult(equipNums,checkId,fourthId);
    }

    /**
     * @author: liukang
     * @Description: 导入excel文件
     * @date: 2020/7/21 23:18
     * @param:
     * @return:
     */
    @PostMapping("/importMachineInfoExcel")
    public String importMachineInfoExcel(
            @RequestParam("file")MultipartFile file,
            @RequestParam("projectId") int projectId,
            @RequestParam("projectType") int projectType,
            @RequestParam("equipType") int equipType,
            @RequestParam("machineType") int machineType
            ){
        Date date = new Date();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        String fileName = time+file.getOriginalFilename();
        try{
            /*   上传文件路径：当前路径/upload/importfiles/...    */
            String path = FileUtil.upload(file.getInputStream(), "importfiles/", fileName);
            String filePath = path+fileName;
            System.out.println("filepath："+filePath);
            return equipService.importMachineInfoExcel(filePath,projectId,projectType,equipType,machineType);
        }catch(Exception e){
            e.printStackTrace();
            return "error：文件导入出错";
        }
    }

    /**
     * @author: liukang
     * @Description: 录入
     * @date: 2020/6/21 11:08
     * @param: machineinfo
     * @return: "success" or "error"
     */
    @PostMapping("/saveMachineInfo")
    public String saveMachineInfo(MachineInfo machineinfo){
        System.out.println(machineinfo.toString());
        return equipService.saveMachineInfo(machineinfo);
    }

    /**
     * @author: liukang
     * @Description: 假删除
     * @date: 2020/6/21 11:08
     * @param: mKey
     * @return: "success" or "error"
     */
    @PostMapping("/deleteMachInfoByNY")
    public String deleteMachInfoByNY(Integer mKey){
        return equipService.deleteMachInfoByNY(mKey);
    }

    /**
     * @author: liukang
     * @Description: 编辑
     * @date: 2020/6/21 11:04
     * @param: machineinfo
     * @return: "success" or "error"
     */
    @PostMapping("/updateMachineInfo")
    public String updateMachineInfo(MachineInfo machineinfo){
        System.out.println(machineinfo);
        return equipService.updateMachineInfo(machineinfo);
    }

    /**
     * @author: liukang
     * @Description: 根据工程id获取信息，即展示所有信息时调用的方法
     * @date: 2020/6/21 11:05
     * @param: projectName，pageNum，pageSize
     * @return: List<MachineInfo>
     */
    @GetMapping("/getMachineInfoByProjectName")
    public Object getMachineInfoByProjectId(Integer projectId,int pageNum,int pageSize){
        return equipService.getMachineInfoByProjectId(projectId,pageNum,pageSize);
    }


    /**
     * @author: liukang
     * @Description: 根据功能id和设备类型获取信息，即下拉框选择设备类型时调用此方法
     * @date: 2020/6/21 11:07
     * @param: projectName，equipType，pageNum，pageSize
     * @return: List<MachineInfo>
     */
    @GetMapping("/getMachineInfoByEquipType")
    public Object getMachineInfoByEquipType(Integer projectId,Integer machineType,int pageNum,int pageSize){
        return equipService.getMachineInfoByEquipType(projectId,machineType,pageNum,pageSize);
    }
 /**
  * @author xiaxing
  *
  * @date 2021/3/23 11:31
  * @param: (projectId，equipType，pageNum，pageSize)
  * @return List<MachineInfo>
  */
 @GetMapping("/getMachineInfoByProjectIdAndEquipType")
      public Object getMachineInfoByProjectIdAndEquipType(String projectId,String equipType,int pageNum,int pageSize){

     return equipService.getMachineInfoByProjectIdAndEquipType( Integer.parseInt(projectId),Integer.parseInt(equipType),pageNum,pageSize);
 }


     /**
      * @author: liukang
      * @Description: 搜索时调用的方法
      * @date: 2020/6/21 11:04
      * @param: searchParams，pageNum，pageSize
      * @return: List<MachineInfo>
      */
    @GetMapping("/getMachineInfoBySearchParams")
    public Object getMachineInfoBySearchParams(String searchParams,int pageNum,int pageSize){
        return equipService.getMachineInfoBySearchParams(searchParams,pageNum,pageSize);
    }

    /**
     * @author: liukang
     * @Description: 在体系里根据number拿到parametername
     * @date: 2020/7/23 12:23
     * @param:
     * @return:
     */
    @GetMapping("/getName4ByName4Id")
    public String getName4ByName4Id(int number){
        return equipService.getName4ByName4Id(number);
    }

    /**
     * @author： wenyihan
     * 获取工程信息
     * @date： 11:48 2020/6/22
     * @param： []
     * @return： java.lang.Object
    */
    @PostMapping("/getProjectNameByTaskId")
    public List<ContractCheckTask> getProjectNameByTaskId(int taskId){
        return equipService.getProjectNameByTaskId(taskId);
    }

    /**
     * @author： wenyihan
     * 通过projectName获取工程类别
     * @date： 20:27 2020/6/23
     * @param： [projectName]
     * @return： java.lang.String
    */
    @PostMapping("/getProjectCategoryByProjectId")
    public String getProjectCategoryByProjectName(int projectId){
        return equipService.getProjectCategoryByProjectId(projectId);
    }

    /**
     * @author： wenyihan
     * 通过项目id获取项目名称
     * @date： 15:52 2020/6/27
     * @param： [projectId]
     * @return： java.lang.String
    */
    @PostMapping("/getProjectNameByProjectId")
    public String getProjectNameByProjectId(int projectId){
        return equipService.getProjectNameByProjectId(projectId);
    }



    /**
     * @Author chenhuanru
     * @Description //获取无问题设备编号
     * @Date 19:08 2020/6/21
     * @Param [checkId, fourthId]
     * @return java.lang.Object
     **/

    @PostMapping("/getNoPromblemEquipNum")
    public Object getNoPromblemEquipNum(int checkId,int fourthId){

        return equipService.getNoPromblemEquipNum(checkId,fourthId);
    }

    /**
     * @author： wenyihan
     * @description：通过clientId获取片区信息
     * @date： 12:15 2020/7/12
     * @param： [clientId]
     * @return： java.lang.Object
    */
    @PostMapping("/getTaskOptionsByClientId")
    public List<Contracttaskaccept> getTaskOptionsByClientId(Integer contractId){
        return equipService.getTaskOptionsByClientId(contractId);
    }

    /**
     * @author： wenyihan
     * @description：获取设备检查体系下的第二级体系
     * @date： 16:02 2020/7/22
     * @return： java.util.List<com.whut.model.Prefixsystem>
    */
    @PostMapping("/getSecondLevel")
    public List<ParPrefixSystem> getSecondLevel(){
        return equipService.getSecondLevel();
    }

    @PostMapping("/getThirdAndFourthLevel")
    public List<ParPrefixSystem> getThirdAndFourthLevel(int number){
        return equipService.getThirdAndFourthLevel(number);
    }

    /**
     * @Author chenhuanru
     * @Description //获取单台设备未涉及项的方法
     * @Date 12:32 2020/6/29
     * @Param [eqipNum, checkId, fourthId]
     * @return java.lang.Object
     **/
    @PostMapping("/getEquipUncheckedItems")
    public Object getEquipUncheckedItems(String eqipNum,int checkId,int fourthId){
        return equipService.getEquipUncheckedItems(eqipNum,checkId,fourthId);
    }

    /**
     * @author： wenyihan
     * @description：在ps表中通过number获取parameterName
     * @date： 23:54 2020/7/24
     * @param： [number]
     * @return： java.lang.String
    */
    @PostMapping("/getParameterNameByNumber")
    public String getParameterNameByNumber(int number){
        return equipService.getParameterNameByNumber(number);
    }

    @GetMapping("/getMachineInfoRecordByProjectId")
    public Object getMachineInfoRecordByProjectId(int projectId){
        return equipService.getMachineInfoRecordByProjectId(projectId);
    }

    /**
     * @author： wenyihan
     * @description：通过父id和parameterName查询当前选择体系的number
     * @date： 18:53 2020/7/31
     * @param： [pName, preNum]
     * @return： int
     */
    @PostMapping("/getPSNumByPNameAndPreNum")
    public int getParameterNameByPreNumAndNum(String pName, int preNum){
        return equipService.getParameterNameByPreNumAndNum(pName, preNum);
    }

    /**
     * Author:yjh
     * Description:通过委托+任务+项目（checkId）获取所有设备信息
     * Create date:2021/3/4 14:17
     * Param:[client,task,check,startTime,endTime]
     * Return:设备信息列表
     * Modify[]:,,
     */
    @ResponseBody
    @PostMapping("/getAllequipInfoByClientTaskCheckid")
    public Object getAllequipInfoByClientTaskCheckid(String client, String task, String check, String startTime, String endTime){
        return equipService.getAllequipInfoByClientTaskCheckid(client,task,check,startTime,endTime);
    }

    /**
     * Author:yjh
     * Description:根据checkID查询所有equipscore,若没有直接查询到会联立查询并插入表内
     * Create date:2021/3/10 15:14
     * Param:[String check]
     * Return:Object[List<Equipscore>]
     * Modify[]:,,
     */
    @ResponseBody
    @PostMapping("/getAllequipScoreByCheckId")
    public Object getAllequipScoreByCheckId(String check){
        return equipService.getAllEquipscoreBycheckId(check);
    }

    /**
     * Author:yjh
     * Description:通过checkid&equiptype直接在EquipScore数据库中查询
     * Create date:2021/3/10 15:50
     * Param:[String check]
     * Return:Object
     * Modify[]:,,
     */
    @ResponseBody
    @PostMapping("/getEquipscoreBycheckIdEquipType")
    public Object getEquipscoreBycheckIdEquipType(String check,String equipType){
        return equipService.getEquipscoreBycheckIdEquipType(check,equipType);
    }
    /**
     * Author:yjh
     * Description:此方法返回通过checkId查询的设备类型（equipType），并做关于checkid的增删改操作
     * Create date:2021/3/11 17:22
     * Param:[String check]
     * Return:Object
     * Modify[0]:2021/4/12,yjh,此方法返回通过checkId查询的设备类型（equipType），并做关于checkid的增删改操作
     */
    @ResponseBody
    @PostMapping("/getEquiptypeByCheckId")
    public Object getEquiptypeByCheckId(String check){
        equipService.getAllEquipscoreBycheckId(check);
        return equipService.getEquiptypeByCheckId(check);
    }
   @ResponseBody
    @PostMapping("/getEquipNUmByProjectIdAndEquipType")
   public Object getEquipNUmByProjectIdAndEquipType(int projectId,int equipType)
       {
        return equipService.getEquipNUmByProjectIdAndEquipType(projectId,equipType);
    }
}
